#!/bin/bash
#
# This shell script takes care of starting and stopping DTNd.
# Derived from 
# http://sophie.zarb.org/distrib/Mandriva/cooker//srpms/dtn/files/7
#
# chkconfig: 345 55 45
# description: A Delay Tolerant Networking (DTN) reference implementation
# processname: dtnd
# pidfile: /var/run/dtnd.pid
# config: /etc/dtn.conf
# config: /etc/sysconfig/dtnd

### set -x
echo "Starting dtn-daemon script" > /tmp/dtn-daemon.out

# DTN daemon need shared library stuff...
export LD_LIBRARY_PATH=/home/dorado1/xerces-c_2_8_0-x86-linux-gcc_3_4//lib:/usr/local/lib

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

DTN_HOME=/home/dorado1/mydtn

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0

[ -f /etc/dtn.conf ] || exit 0

[ -f /usr/bin/dtnd ] || exit 0

[ -f /etc/sysconfig/dtnd ] && . /etc/sysconfig/dtnd

# See how we were called.
case "$1" in
start)
        echo "Got start command" >> /tmp/dtn-daemon.out
	if [ -n "`/sbin/pidof dtnd`" ]; then
	    echo -n "DTN daemon already running"
            echo "DTN daemon alrady running" >> /tmp/dtn-daemon.out
	    echo
            exit 1
        fi
	echo -n "Starting DTN daemon: "
###	/usr/bin/dtnd -d -o $DTN_HOME/dtnd.log -l debug -c $DTN_HOME/trex.dtn.conf &
	daemon /usr/bin/dtnd --daemonize \
	${DTND_OUTPUT:-"--output $DTN_HOME/dtnd.log"} \
	${DTND_LEVEL:-"-l debug"} \
	${DTND_CONF:-"--conf $DTN_HOME/trex.dtn.conf"} \
	${DTND_OPTIONS:-"-t"}
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/dtnd
        echo "RETVAL: $RETVAL" >> /tmp/dtn-daemon.out
	;;
stop)
	echo -n "Stopping DTN daemon: "
	killproc dtnd
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/dtnd
	;;
status)
	status dtnd
	RETVAL=$?
	;;
restart|reload)
	$0 stop
	$0 start
	RETVAL=$?
	;;
condrestart)
	[ -f /var/lock/subsys/dtnd ] && restart
	;;
  *)
	echo "Usage: $0 {start|stop|status|restart|condrestart|reload}"
	exit 1
esac

exit $RETVAL
